﻿using System;
using System.Collections.Generic;
using System.Text;
using Notepad_X;
using Notepad_X.Plugin;
using Notepad_X.Classes;
using System.Windows.Forms;


namespace SideBarPlugin
{
    public class plugin : IPlugin
    {
        Main main;
        public SplitContainer SidebarSplit;
        ComboBox PluginsBox;
        Panel SidebarContainer;
        Panel PluginsMenu;
        public System.Collections.Hashtable Controls;

        public string Name
        {
            get { return "Sidebar Plugin"; }
        }
        public string Author
        {
            get { return "NohponeX"; }
        }
        public string Description
        {
            get { return "Sidebar Plugin Holder"; }
        }
        public string Version
        {
            get { return "1.1"; }
        }

        public bool HasSettings {
            get { return false; }
        }

        public void Initialize()
        {
            main = Static.MainInstance;
            Controls = new System.Collections.Hashtable();
            SidebarSplit = new SplitContainer();
            main.Controls.Add(SidebarSplit);
            SidebarSplit.Dock = DockStyle.Fill;
            SidebarSplit.BringToFront();
            main.Panel.Parent = SidebarSplit.Panel1;

            SidebarSplit.SplitterDistance = main.Width - 175;
            // main resize add handler
            main.Resize += new EventHandler(main_Resize);
       
            PluginsBox = new ComboBox();
            PluginsBox.DropDownStyle = ComboBoxStyle.DropDownList;

            PluginsMenu = new Panel();
           
            PluginsBox.SelectedIndexChanged += new EventHandler(box_SelectedIndexChanged);

            SidebarSplit.Panel2.Controls.Add(PluginsBox);

            SidebarContainer = new Panel();
            PluginsMenu.Controls.Add(PluginsBox);
            SidebarSplit.Panel2.Controls.Add(PluginsMenu);
            PluginsMenu.Location = new System.Drawing.Point(10, 5);
            
           
            PluginsMenu.Dock = DockStyle.Top;
            PluginsBox.Location = new System.Drawing.Point(10, 5);
            PluginsMenu.Size = new System.Drawing.Size(130, 35);
            SidebarSplit.Panel2.Controls.Add(SidebarContainer);
            //container.Location = new System.Drawing.Point(10, 50);
            SidebarContainer.Dock = DockStyle.Fill;
            SidebarContainer.BringToFront();

            //Check every plugin if contains Notepad X sidebar and reactive it

            foreach(AvailablePlugin PlugIn in Notepad_X.Plugin.Global.Plugins.AvailablePlugins){
                if (PlugIn.Instance.Description.Contains("Notepad X Sidebar Plugin"))
                {
                    PlugIn.Instance.Initialize();
                }
            }
        }
        public void Settings() { }

        void box_SelectedIndexChanged(object sender, EventArgs e)
        {
            SidebarContainer.Controls.Clear();
            SidebarContainer.Controls.Add((UserControl)Controls[PluginsBox.SelectedItem.ToString()]);
            //((GroupBox)controls["TestSideBarPlugin"]).Location = new System.Drawing.Point(0, 0);
            //((GroupBox)controls["TestSideBarPlugin"]).Dock = DockStyle.Fill;
            //((GroupBox)controls["TestSideBarPlugin"]).CreateControl();
        }
        void main_Resize(object sender, EventArgs e)
        {
            if (main.Width > 200)
            {
                SidebarSplit.SplitterDistance = main.Width - 175;
                SidebarSplit.Visible = true;
            }else{
                SidebarSplit.Visible = false;
            }
        }
        /// <summary>
        /// Add an UserControl Control to the Sidebar , UserControl name will be used as key at the sidebar
        /// </summary>
        /// <param name="groupBox"></param>
        public void AddSidebarPlugin(UserControl control)
        {
            PluginsBox.Items.Add(control.Name);
            Controls.Add(control.Text, control);
        }
        /// <summary>
        /// Remove an GroupBox Control from the Sidebar
        /// </summary>
        /// <param name="groupBox"></param>
        public void RemoveSidebarPlugin(UserControl control)
        {
            try {
                if (PluginsBox.SelectedItem.ToString() == control.Name) {
                    SidebarContainer.Controls.Clear();
                }
                PluginsBox.Items.Remove(control.Name);
                Controls.Remove(control.Text);
            } catch (Exception) {
            }
        }
        public GroupBox[] GetControls()
        {
            GroupBox[] toReturn = new GroupBox[Controls.Count];
            int i = 0;
            foreach (GroupBox Control in Controls.Values)
            {
                toReturn[i] = Control;
            }
            return toReturn;
        }
        public void Dispose()
        {
            main.Panel.Parent = main;
            main.Panel.BringToFront();
            main.Controls.Remove(SidebarSplit);

            foreach (AvailablePlugin PlugIn in Notepad_X.Plugin.Global.Plugins.AvailablePlugins)
            {
                if (PlugIn.Instance.Description.Contains("Notepad X Sidebar Plugin"))
                {
                    PlugIn.Instance.Dispose();
                }
            }

        }
    }
    public static class Functions
    {
        public static SideBarPlugin.plugin GetSidebarPlugin()
        {
            SideBarPlugin.plugin toReturn = null;
            if (Notepad_X.Plugin.Global.Plugins.colAvailablePlugins.Exist("Sidebar Plugin"))
            {
                toReturn = (SideBarPlugin.plugin)Notepad_X.Plugin.Global.Plugins.colAvailablePlugins.Find("Sidebar Plugin").Instance;
            }
            return toReturn;
        }
    }

}
